<template>
  <el-drawer
    v-model="visible"
    size="820px"
    class="audit-drawer"
    title="复购单财务审核"
    :close-on-click-modal="false"
  >
    <!-- 基础信息 -->
    <div class="section-title">
      <span class="label">基础信息</span>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="原始单号" :span="2">{{
          form.orderId || '无'
        }}</el-descriptions-item>
        <el-descriptions-item label="客户姓名">{{ form.receiverName }}</el-descriptions-item>
        <el-descriptions-item label="客户电话">{{ form.receiverPhone }}</el-descriptions-item>
        <el-descriptions-item label="收货地址" :span="2">{{
          form.receiverAddress
        }}</el-descriptions-item>
        <el-descriptions-item label="订单时间">{{ form.orderTime }}</el-descriptions-item>
        <el-descriptions-item label="是否大单">{{ form.isBig ? '是' : '否' }}</el-descriptions-item>

        <el-descriptions-item label="订单备注">{{ form.remark || '无' }}</el-descriptions-item>
      </el-descriptions>
    </div>

    <!-- 产品明细 -->
    <div class="section-title">
      <span class="label">产品明细</span>
      <el-table :data="form.productCollection" border size="small">
        <el-table-column label="产品名称" prop="productName" />
        <el-table-column label="产品类型" prop="usageType" :formatter="getGoodsTypeByUsageType">
        </el-table-column>
        <el-table-column label="件数" prop="num" width="120" />
      </el-table>
      <div class="total-bar">
        合计：<b>{{ totalQty }}</b> 件， <b class="amount">{{ form.actualPrice }}</b> 元
      </div>
    </div>

    <!-- 付款信息 -->
    <div class="section-title">
      <span class="label">付款信息</span>
      <el-descriptions :column="2" border>
        <el-descriptions-item label="付款方式">{{ form.paymentMethod }}</el-descriptions-item>
        <el-descriptions-item label="总金额">{{ form.totalAmount }} RMB</el-descriptions-item>
        <el-descriptions-item label="已付金额">{{ form.actualPrice }} RMB</el-descriptions-item>
        <el-descriptions-item label="代收金额">{{ form.codAmount }} RMB</el-descriptions-item>
        <el-descriptions-item label="延期发货">{{
          form.isDelayedDelivery ? '是' : '否'
        }}</el-descriptions-item>
        <el-descriptions-item label="物流公司">{{ form.logisticsPlatform }}</el-descriptions-item>
        <el-descriptions-item label="物流单号" span="2">{{
          form.logisticsNo
        }}</el-descriptions-item>
        <el-descriptions-item :span="2" label="附件">
          <ImagePrevie
            v-if="form.fileAttachment"
            :src="form.fileAttachment"
            width="120px"
            height="120px"
          />
          <div v-else style="color: #999">无附件</div>
        </el-descriptions-item>
      </el-descriptions>
    </div>

    <!-- 审核操作 -->
    <div class="section-title">
      <span class="label">财务审核</span>
      <el-form ref="auditFormRef" :model="auditForm" :rules="auditRules" label-width="90px">
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input
            v-model="auditForm.auditRemark"
            type="textarea"
            rows="3"
            placeholder="请输入审核意见"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="footer-btns">
        <el-button @click="visible = false">取消</el-button>
        <el-button :loading="submitLoading" type="primary" @click="handleAuditSubmit"
          >确认审核</el-button
        >
      </div>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { ref, reactive, computed } from 'vue';
import { ElMessage, type FormInstance, type FormRules, type UploadUserFile } from 'element-plus';
import { getImgUrl } from '../../utils/request';
import { verificationOrder } from '../../api/platformOrders/orders/list';

const props = defineProps({
  goodsTypes: {
    type: Array,
    default: () => []
  }
});

const emits = defineEmits(['ok']);

const visible = ref(false);
const form = reactive<any>({
  productCollection: []
});

// 审核表单
const auditFormRef = ref<FormInstance>();
const auditForm = reactive({
  auditRemark: ''
  // auditStatus: "",
});
const auditRules: FormRules = {
  auditRemark: [{ required: true, message: '请输入审核备注', trigger: 'blur' }]
  // auditStatus: [
  //   { required: true, message: "请选择审核结果", trigger: "change" },
  // ],
};

// 统计
const totalQty = computed(
  () => form.productCollection?.reduce((t, p) => t + parseInt(p.num), 0) || 0
);

// 根据产品类型
const getGoodsTypeByUsageType = (row: any) => {
  const type = row.usageType;
  const findx: any = props.goodsTypes.filter((item: any) => item.value == type);
  if (findx.length > 0) {
    return findx[0].label;
  }
  return '';
};

// 显示方法
const show = (orderData: any) => {
  Object.assign(form, orderData);

  visible.value = true;
};

// 隐藏
const hide = () => {
  visible.value = false;
  auditForm.auditRemark = '';
  // auditForm.auditStatus = "";
};

const submitLoading = ref(false);
const handleAuditSubmit = () => {
  auditFormRef.value?.validate((valid) => {
    if (valid) {
      submitLoading.value = true;
      visible.value = false;
      verificationOrder({
        orderId: form.orderId,
        verRemark: auditForm.auditRemark
      })
        .then((res) => {
          submitLoading.value = false;
          if (res.code == 200) {
            ElMessage.success('核销成功');
            emits('ok');
            hide();
          }
        })
        .catch(() => {
          submitLoading.value = false;
        });
    }
  });
};

defineExpose({
  show,
  hide
});
</script>

<style scoped lang="scss">
.section-title {
  border: 1px solid #ebebeb;
  padding: 40px 20px 20px 20px;
  border-radius: 4px;
  position: relative;
  margin-bottom: 10px;
  background: #fafafa;

  .label {
    position: absolute;
    display: inline-block;
    padding: 5px 10px;
    background: #ebebeb;
    font-size: 13px;
    border-radius: 0 4px 4px 0;
    left: 0;
    top: 0;
  }
}
.total-bar {
  text-align: right;
  margin-top: 10px;
  font-size: 15px;
  font-weight: 500;
  color: #333;
  border-top: 1px solid #eee;
  padding-top: 8px;
  .amount {
    color: #f56c6c;
    font-weight: 700;
  }
}
.remark-box {
  background: #fff;
  padding: 10px;
  min-height: 60px;
  border-radius: 4px;
  border: 1px solid #eee;
  font-size: 14px;
  color: #555;
}
.image-list {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.footer-btns {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}
</style>
